package com.feilo.main;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.List;

import org.apache.poi.sl.usermodel.AutoShape;
import org.apache.poi.sl.usermodel.TextRun;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;

public class Test1 {
	public static void main(String[] args) throws IOException {
		Calendar cale = Calendar.getInstance();
		int year = cale.get(Calendar.YEAR);
		int month = cale.get(Calendar.MONTH);// 月份从0开始

		// 导入待拼接的.pptx文件
		InputStream coveris = new FileInputStream(new File("C:\\Users\\12456\\Desktop\\cover.pptx"));
		InputStream datais = new FileInputStream(new File("C:\\Users\\12456\\Desktop\\201807.pptx"));

		XMLSlideShow coverppt = new XMLSlideShow(coveris);// 待拼接PPT1
		XMLSlideShow datappt = new XMLSlideShow(datais);// 待拼接PPT2
		XMLSlideShow headppt = getPPT(); // 此方法返回待拼接PPT3

		// 将PPT3依次插入到PPT2的末尾，createSlide()是在末尾创建
		for (XSLFSlide srcSlide : headppt.getSlides()) {
			datappt.createSlide().importContent(srcSlide);
		}
		// 下面代码 是将当前月份的数值所对应ppt中的第几张 插入到PPT2的末尾
		List<XSLFSlide> coverlist = coverppt.getSlides();
		for (int i = 0; i < coverlist.size(); i++) {
			if (i == month) {
				datappt.createSlide().importContent(coverlist.get(i));
			}
		}
		// 此时PPT1，PPT3都已插入到PPT2中，现在进行重新排序， setSlideOrder(第几张PPT（slide）,
		// 要去的位置（int）)；
		datappt.setSlideOrder(datappt.getSlides().get(7), 0);
		datappt.setSlideOrder(datappt.getSlides().get(5), 1);
		datappt.setSlideOrder(datappt.getSlides().get(6), 2);

		// 以下为导出PPT的操作
		String filePath = "C:\\Users\\12456\\Desktop\\pptresult";
		filePath = filePath.replace("\\", "\\\\");

		File dir = new File(filePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		String path = dir.getPath();
		path = path.replace("\\", "\\\\");

		FileOutputStream out = new FileOutputStream(path + "\\天气会商制作.pptx");
		datappt.write(out);
		out.close();
	}

	public static XMLSlideShow getPPT() throws IOException {
		// 导入要修改的PPT
		InputStream is = new FileInputStream(new File("C:\\Users\\12456\\Desktop\\服务简报内容\\模板.pptx"));
		XMLSlideShow ppt = new XMLSlideShow(is);
		List<XSLFSlide> Slides = ppt.getSlides();
		for (XSLFSlide slide : Slides) {
			List<XSLFShape> shapes = slide.getShapes();
			for (XSLFShape shape : shapes) {
				if (shape != null) {
					if (shape instanceof AutoShape) {
						try {
							if (((AutoShape) shape).getText().contains("{ye}")) {
								// 替换文字内容.用TextRun获取替换的文本来设置样式

								TextRun rt = ((AutoShape) shape)
										.setText(((AutoShape) shape).getText().replace("{ye}", "2018"));

								rt.setFontColor(Color.red);
								rt.setFontSize(13.5);
								rt.setFontFamily("微软雅黑");

							} else if (((AutoShape) shape).getText().contains("{mo}")) {
								((AutoShape) shape).setText(((AutoShape) shape).getText().replace("{mo}", "7"));
							}
							// System.out.println(((AutoShape)shape).getText());
						} catch (Exception ex) {

							ex.printStackTrace();
						}
						// }else {
						// System.out.println("Process me: " +
						// shape.getClass());
					}
				}
			}
		}
		return ppt;
	}

}
